Git
CLI
tool
versioning
Git is a version control system mostly used for code.
Configuration
Description | Command |
---|---|
Set username (for your commits) | git config --global user.name "myname" |
Set email (for your commits) | git config --global user.email "my@email.com" |
Enable CLI color codes | git config --global color.ui auto |
New repositories
- Download git repo from server
-
git clone url.com
Getting started
Description | Command |
---|---|
Initialize a new git repository | git init my-repo |
Get the status of changes (staged, unstaged, untracked) | git status |
Track files to staging | git add . |
Remove files from staging | git reset <file> |
Commit staged files | git commit -m "informative commit message" |
Discard changes in a file (!: Your changes will be lost) | git restore <file> |
Working with remote repositories
Description | Command |
---|---|
Clone a remote repository | git clone url.com |
Push changes to remote repository | git push |
Pull changes from remote repository | git pull |
Changes
Code change workflow
- Get changes from remote repository
-
git fetch
- Check differences to branch you want merge into yours
-
git diff feature1..dev
- Merge changes from remote branch into your branch
-
git merge origin master
- Check what files are(n’t) staged for commit
-
git status
- Stage all files that are tracked (not in .gitignore)
-
git add .
- If necessary: Unstage all files (or a specific file) that are staged
-
git reset [specific-file]
- Commit all staged files (add to versioning history)
-
git commit -m "[ticket-id + what you changed]"
- Load commits into remote git repository
-
git push
Managing changes
- See changes due to last pull request
-
git log -p -2
Pull changes into dev/test/master branch
- Pull changes into dev branch (and add your changes at the end)
-
git fetch git checkout dev git pull --rebase feature1branch
- Pull only specific commits into your branch
-
git cherry-pick [commit-SHA]
Undo and Rollback
- Rollback the last commit
-
git reset head~1
To provide the files from the last commit: git reset --soft HEAD~1
- Rollback to a specific commit
-
You can still see the changes from your HEAD in your files. To make the reset materialize, you need to discard these unstaged changes in git.
git reset [commit-SHA]
Since you are now several commits behind the remote, you have to force push the changes: git push -f origin [myBranchyBranch]
.
To create a new commit to undo earlier commits, use git revert
.
- Travel back to a specific commit
-
git checkout [commit-SHA]
- Delete sensitive data from history:
- docs.github.com
Branches
Description | Command |
---|---|
List all branches and show which one you are on | git branch -a |
Create new branch and check it out | git checkout -b [branch-name] |
Change to a specific branch | git checkout [specific-branch] |
Delete a specific branch | git branch -d [specific-branch] |
Rename the current branch | git branch -m myNewBranch |
Show commit history with branch dependencies | git log --graph --oneline |